package common

const (
	SuccessCode       int = 200
	InvalidTokenCode      = 1024
	InvalidParamsCode int = 1000 + iota
	RefreshCaptchaCode
)

const (
	CodeSuccess        string = "success"
	CodeRefreshCaptcha string = "请勿频繁刷新"
)

type CodeError struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

// ResponseData 此处结构体用于统一返回结果
type ResponseData struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data,omitempty"`
}

func NewCodeError(code int, msg string) error {
	return &CodeError{Code: code, Msg: msg}
}

// NewInvalidParamsError 设置接口参数校验错误码
func NewInvalidParamsError(msg string) error {
	return NewCodeError(InvalidParamsCode, msg)
}

func (c *CodeError) Error() string {
	return c.Msg
}

func (c *CodeError) Data() *ResponseData {
	return &ResponseData{
		Code: c.Code,
		Msg:  c.Msg,
	}
}

// NewResponseData 接口请求成功返回数据
func NewResponseData(data interface{}) *ResponseData {
	return &ResponseData{
		Code: SuccessCode,
		Msg:  CodeSuccess,
		Data: data,
	}
}
